﻿using AutoMapper;

using Microsoft.IdentityModel.Tokens;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Models;
using ZFB.Repository.AdminUser;
using ZFB.Repository.Role;

namespace ZFB.Services
{
    public class RoleServices : IRoleServices
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IAdminUserRepository _adminUserRepository;
        private readonly IMapper _mapper;
        public RoleServices(IRoleRepository roleRepository, IAdminUserRepository adminUserRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            _adminUserRepository = adminUserRepository;
            _mapper = mapper;
        }

        public async Task<ApiResult> AddNewAsync(string roleName)
        {
            if (await _roleRepository.IsExistAsync(e => e.Name == roleName)) return new("角色已存在");
            var res = await _roleRepository.AddAsync(new T_Roles
            {
                Name = roleName,
            });
            return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();

        }

        public async Task<ApiResult> AddRoleIdsAsync(long adminUserId, long[] roleIds)
        {
            var user = await _adminUserRepository.GetModelAsync(e => e.Id == adminUserId);
            if (user == null) return new ApiResult("adminuser不存在");
            var role = await _roleRepository.GetListAsync(e => roleIds.Contains(e.Id));
            foreach (var item in role)
            {
                user.T_Roles.Add(item);
            }
            var res = await _adminUserRepository.UpdateAsync(user);
            return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();
        }

        public async Task<ApiResult> GetAllAsync()
        {
            var data = await _roleRepository.GetListAsync();
            if (data.IsNullOrEmpty()) return new ApiResult("获取失败");
            var dto = _mapper.Map<List<RoleDTO>>(data);
            return new ApiResult(dto);

        }

        public async Task<ApiResult> GetByAdminUserIdAsync(long adminUserId)
        {
            var user = await _adminUserRepository.GetListAsync(e => e.Id == adminUserId);
            if (user.IsNullOrEmpty()) return new ApiResult("获取失败!");
            var role = user.SelectMany(e => e.T_Roles);
            var dto = _mapper.Map<List<RoleDTO>>(role);
            return new ApiResult(dto);
        }

        public async Task<ApiResult> GetByIdAsync(long id)
        {
            var user = await _roleRepository.GetModelAsync(e => e.Id == id);
            if (user == null) return new ApiResult("role不存在");
            var dto = _mapper.Map<RoleDTO>(user);
            return new ApiResult(dto);
        }

        public async Task<ApiResult> GetByNameAsync(string name)
        {
            var user = await _roleRepository.GetModelAsync(e => e.Name == name);
            if (user == null) return new ApiResult("name不存在");
            var dto = _mapper.Map<RoleDTO>(user);
            return new ApiResult(dto);
        }

        public async Task<ApiResult> MarkDeletedAsync(long roleId)
        {
            if (!await _roleRepository.IsExistAsync(e => e.Id == roleId))
                return new ApiResult("role不存在");
            var res = await _roleRepository.UpdateIsDeleteAsync(e => e.Id == roleId);
            return res <= 0 ? new ApiResult("删除失败！") : new ApiResult();
        }

        public async Task<ApiResult> UpdateAsync(long roleId, string roleName)
        {
            var user = await _roleRepository.GetModelAsync(e => e.Id == roleId);
            if (user == null) return new ApiResult("role不存在");
            if (await _roleRepository.IsExistAsync(e => e.Name == roleName && e.Id != roleId))
                return new ApiResult("roleName已存在");
            user.Name = roleName;
            var res = await _roleRepository.UpdateAsync(user);
            return res <= 0 ? new ApiResult("更新失败！") : new ApiResult();
        }

        public async Task<ApiResult> UpdateRoleIdsAsync(long adminUserId, long[] roleIds)
        {
            var users = await _adminUserRepository.GetModelAsync(e => e.Id == adminUserId);
            if (users == null) return new ApiResult("adminuser不存在");
            users.T_Roles.Clear();
            var roles = await _roleRepository.GetListAsync(e => roleIds.Contains(e.Id));

            roles.ForEach(r => users.T_Roles.Add(r));

            var res = await _adminUserRepository.UpdateAsync(users);
            return res <= 0 ? new ApiResult("更新失败！") : new ApiResult();
        }
    }
}
